package com.bvbot.core.bot;

import static com.bvbot.core.Logger.log;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import com.bvbot.core.BvBotDateFormatter;
import com.bvbot.core.domain.AbstractDomainObject;

public class Prices extends AbstractDomainObject {

	private Float goldWorldValue;
	private Float silverWorldValue;

	private Float bvGoldBuyValue;
	private Float bvGoldSellValue;

	public Float getSilverWorldValue() {
		return silverWorldValue;
	}

	public void setSilverWorldValue(Float silverValue) {
		this.silverWorldValue = silverValue;
	}

	public Float getBvGoldBuyValue() {
		return bvGoldBuyValue;
	}

	public void setBvGoldBuyValue(Float goldBuyValue) {
		this.bvGoldBuyValue = goldBuyValue;
	}

	public Float getBvGoldSellValue() {
		return bvGoldSellValue;
	}

	public void setBvGoldSellValue(Float goldSellValue) {
		this.bvGoldSellValue = goldSellValue;
	}

	public Float getBvSilverSellValue() {
		return bvSilverSellValue;
	}

	public void setBvSilverSellValue(Float silverSellValue) {
		this.bvSilverSellValue = silverSellValue;
	}

	public Float getBvSilverBuyValue() {
		return bvSilverBuyValue;
	}

	public void setBvSilverBuyValue(Float silverBuyValue) {
		this.bvSilverBuyValue = silverBuyValue;
	}

	private Float bvSilverSellValue;
	private Float bvSilverBuyValue;

	private Date priceDate;

	public Prices() {
		super();
	}

	public Prices(Float priceValue, Date priceDate) {
		super();
		this.goldWorldValue = priceValue;
		this.priceDate = priceDate;
	}

	public Float getGoldWorldValue() {
		return goldWorldValue;
	}

	public void setGoldWorldValue(Float priceValue) {
		this.goldWorldValue = priceValue;
	}

	public Date getPriceDate() {
		return priceDate;
	}

	public void setPricesDate(Date priceDate) {
		this.priceDate = priceDate;
	}

	@Override
	protected String getUpdateSql() {
		return "update price where id=" + getId() + " set price="
				+ getGoldWorldValue() + ",date="
				+ getSqlStringFor(getPriceDate());
	}

	@Override
	protected String getInsertSql() {
		return "insert into price (price,date) values (" + getGoldWorldValue()
				+ "," + getSqlStringFor(getPriceDate()) + ")";
	}

	@Override
	public Prices fromDb(ResultSet rs) throws IOException {
		Prices result = null;
		try {
			float dbPriceValue = rs.getFloat("price");
			Date dbPriceDate = rs.getDate("date");
			long id = rs.getLong("id");
			result = new Prices(dbPriceValue, dbPriceDate);
			result.setId(id);
		} catch (SQLException e) {
			log("Impossible to read a Price from this resultset : " + rs
					+ " : " + e.getMessage(), e);
		}
		return result;
	}

	@Override
	public String toString() {
		return "gold=" + getGoldWorldValue() + "gold_buy="
				+ getBvGoldBuyValue() + "gold_sell=" + getBvGoldSellValue()
				+ "silver=" + getSilverWorldValue() + "silver_buy="
				+ getBvSilverBuyValue() + "silver_sell="
				+ getBvSilverSellValue() + "@"
				+ BvBotDateFormatter.get().format(getPriceDate());
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((bvGoldBuyValue == null) ? 0 : bvGoldBuyValue.hashCode());
		result = prime * result
				+ ((bvGoldSellValue == null) ? 0 : bvGoldSellValue.hashCode());
		result = prime
				* result
				+ ((bvSilverBuyValue == null) ? 0 : bvSilverBuyValue.hashCode());
		result = prime
				* result
				+ ((bvSilverSellValue == null) ? 0 : bvSilverSellValue
						.hashCode());
		result = prime * result
				+ ((goldWorldValue == null) ? 0 : goldWorldValue.hashCode());
		result = prime * result
				+ ((priceDate == null) ? 0 : priceDate.hashCode());
		result = prime
				* result
				+ ((silverWorldValue == null) ? 0 : silverWorldValue.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Prices other = (Prices) obj;
		if (bvGoldBuyValue == null) {
			if (other.bvGoldBuyValue != null)
				return false;
		} else if (!bvGoldBuyValue.equals(other.bvGoldBuyValue))
			return false;
		if (bvGoldSellValue == null) {
			if (other.bvGoldSellValue != null)
				return false;
		} else if (!bvGoldSellValue.equals(other.bvGoldSellValue))
			return false;
		if (bvSilverBuyValue == null) {
			if (other.bvSilverBuyValue != null)
				return false;
		} else if (!bvSilverBuyValue.equals(other.bvSilverBuyValue))
			return false;
		if (bvSilverSellValue == null) {
			if (other.bvSilverSellValue != null)
				return false;
		} else if (!bvSilverSellValue.equals(other.bvSilverSellValue))
			return false;
		if (goldWorldValue == null) {
			if (other.goldWorldValue != null)
				return false;
		} else if (!goldWorldValue.equals(other.goldWorldValue))
			return false;
		if (priceDate == null) {
			if (other.priceDate != null)
				return false;
		} else if (!priceDate.equals(other.priceDate))
			return false;
		if (silverWorldValue == null) {
			if (other.silverWorldValue != null)
				return false;
		} else if (!silverWorldValue.equals(other.silverWorldValue))
			return false;
		return true;
	}

}
